#include <tinx/task.h>
#include <tinx/signal.h>
#include <tinx/string.h>

int sys_sgetmask()
{
    task_t *task = running_task();
    return task->blocked;
}

int sys_ssetmask(int newmask)
{
    task_t *task = running_task();
    int oldmask = task->blocked;
    task->blocked = newmask & ~SIGMASK(SIGKILL);
    return oldmask;
}

int sys_signal(int sig, int handler, int restorer)
{
    if (sig > MAXSIG || sig < MINSIG || sig == SIGKILL)
        return EOF;
    task_t *task = running_task();
    sigaction_t *action = &task->actions[sig - 1];
    action->mask = 0;
    action->handler = (void *)handler;
    action->flags = SIG_ONESHOT | SIG_NOMASK;
    action->restorer = (void *)restorer;
    return EOK;
}

int sys_sigaction(int sig, sigaction_t *action, sigaction_t *old)
{
    if (sig > MAXSIG || sig < MINSIG || sig == SIGKILL)
        return EOF;
    task_t *task = running_task();
    sigaction_t *ptr = &task->actions[sig - 1];
    if (old)
        *old = *ptr;
    *ptr = *action;
    if (ptr->flags & SIG_NOMASK)
        ptr->mask = 0;
    else
        ptr->mask |= SIGMASK(sig);
    return EOK;
}

int sys_kill(pid_t pid, int sig)
{
    if (sig > MAXSIG || sig < MINSIG)
        return EOF;

    
}

void task_signal()
{

}